/*
 *  HTTPRequestHelper.h
 *  AMPlugin
 *
 *  Created by Tiet Anh Vu on 5/24/12.
 *  Copyright 2012 __MyCompanyName__. All rights reserved.
 *
 */

#include <iostream>
//#include <string>
#include <curl/curl.h>
#include <curl/types.h>
#include <curl/easy.h>
#include "json.h"
#include <sys/stat.h>
#include "ProgressBar.h"
#include "IDialogMgr.h"
#define UPLOADCOMMAND								0
#define DOWNLOADCOMMAND								1

using namespace std;

struct myprogress {
	double	lastruntime;
	CURL	*curl;
	long	MinPos;
	long	MaxPos;
	bool	DownUp;
	RangeProgressBar *Progress;
};

class HTTPRequestHelper {
public:
	static string getDataFromURL(const string &url);
	static int loginToCMSServer(RangeProgressBar * DlgProg, int min, int max);
	static string getImage(const string& url, myprogress image_progressbar);
    static string doThreadRequest();
	static void doMultiPerform();
	static string getStoryURL(const string &url, string &error);
	static string getPackageURL(const string &url, string &error);
	static string dump_buffer(void *buffer, int32 buffer_size);
	static string readFileBytes(const string &pathUpload);
	static int UploadFileToCMS(const string &pathFile, bool keep_session = FALSE);
	static int UploadCoverImageToCMS(const string &pathFile, int32 OffsetImg);
	static int progress(void *p, double dltotal, double dlnow, double ultotal, double ulnow);
	//static int PostStoryContentToCMS(const string &storyID, const string &pathFile);
	//static int GetStoryContentFromCMS(const string &storyID, string &errorStr);
	static int PostStoryContentToCMS(const string issueID, const string storyID, const string &pathFile, const string storySessionID);
	static int GetStoryContentFromCMS(const string issueID, const string storyID, string &errorStr, string &sessionIDStr);
	static int UnlockStory(const string issueID, const string storyID);
	static int UnlockPackage(const string issueID, const string pages);
	static int RequestSession();
	static int RequestStorySession(const string &storySessionID);
	static string RenewSession(string Session_id);
	static bool UnlockPackagetoCheckOut(string &error);

	static string getImagePreview(string urlImg, struct myprogress AMPDlgProg);
	static bool	  setProxyServer(CURL *Handlecurl);
	static bool   CallLogCenter(string log_data, string log_ccode, string log_type = "", string log_id = "");
	static void   UpdateStoryStatus();
	static int    PostCaptionContentToCMS(const string captionID, const string &pathFile);
	static void   FinalCheckinCompleted(string final_checkin);
	static int32  DownloadZipfile(string urlRoot);
private: 
	static int writer(char *data, size_t size, size_t nmemb, string *buffer);
	//string jsonParsedString(const string &jsonString);
	static int file_write_response(void *ptr, size_t size, size_t nmemb, void *data);
	static int header_response(void *ptr, size_t size, size_t nmemb, void *data);
	static int progress_func(void* ptr, double TotalToDownload, double NowDownloaded, double TotalToUpload, double NowUploaded);

};